import { ref, onMounted, type Ref, onUnmounted } from 'vue'
import type { FollowType, ConsultOrderItem } from '@/types/consult'
import { followDoctor, getPrescriptionPic, cancelOrder, deleteOrder } from '@/services/consult'
import { ImagePreview, Toast, type FormInstance } from 'vant'
import { OrderType } from '@/enums'
import { getMedicalOrderDetail } from '@/services/order'
import type { OrderDetail } from '@/types/order'
import { setMobileCode } from '@/services/user'
import type { CodeType } from '@/types/user'
//封装逻辑,规范  useXxx 表示使用xxx功能
export const useFollow = (type: FollowType = 'doc') => {
  // 关注功能
  const loading = ref(false)
  // {a, b} 传值的时候  {a, b, c} 也可以传 这是类型的兼容  多的可以给少的
  const follow = async (obj: { id: string; likeFlag: 0 | 1 }) => {
    loading.value = true
    try {
      await followDoctor(obj.id, type)
      obj.likeFlag = obj.likeFlag === 1 ? 0 : 1
    } finally {
      loading.value = false
    }
  }
  return { loading, follow }
}

// 预览处方
export const useShowPrescription = () => {
  const showPrescription = async (id?: string) => {
    if (id) {
      const res = await getPrescriptionPic(id)
      ImagePreview([res.data.url])
    }
  }
  return {
    showPrescription
  }
}

// 取消订单
export const useCancelOrder = () => {
  const loading = ref(false)
  const cancelConsultOrder = (item: ConsultOrderItem) => {
    loading.value = true
    cancelOrder(item.id)
      .then(() => {
        item.status = OrderType.ConsultCancel
        item.statusValue = '已取消'
        Toast.success('取消成功')
        loading.value = false
      })
      .catch(() => {
        Toast.fail('取消失败')
        loading.value = false
      })
    // .finally(() => {
    //   loading.value = false
    // })
  }

  return {
    loading,
    cancelConsultOrder
  }
}

// 删除订单
export const useDeleteOrder = (cb: () => void) => {
  const dedeleLoading = ref(false)
  const deleteConsultOrder = (item: ConsultOrderItem) => {
    dedeleLoading.value = true
    deleteOrder(item.id)
      .then(() => {
        // emit('on-delete', item.id)
        cb && cb()
        // cb   () => {emit('on-delete', item.id)}
        // emit('on-delete', item.id)
        Toast.success('删除成功')
        dedeleLoading.value = false
      })
      .catch(() => {
        Toast.fail('删除失败')
        dedeleLoading.value = false
      })
    // .finally(() => {
    //   dedeleLoading.value= false
    // })
  }
  return {
    dedeleLoading,
    deleteConsultOrder
  }
}

// 获取订单详情
export const useOrderDetail = (id: string) => {
  const loading = ref(false)
  const order = ref<OrderDetail>()
  onMounted(async () => {
    loading.value = true
    const res = await getMedicalOrderDetail(id)
    order.value = res.data
    loading.value = false
  })

  return {
    order,
    loading
  }
}

// 发送验证码逻辑
export const useSendMobileCode = (mobile: Ref<string>, type: CodeType = 'login') => {
  const form = ref<FormInstance>()
  const time = ref(0)
  let timeId: number
  const send = async () => {
    if (time.value > 0) return
    // 验证手机号是否合格
    await form.value?.validate('mobile')
    await setMobileCode(mobile.value, type)
    Toast.success('发送成功')
    time.value = 60
    // 倒计时
    clearInterval(timeId)
    timeId = window.setInterval(() => {
      time.value--
      if (time.value <= 0) window.clearInterval(timeId)
    }, 1000)
  }
  onUnmounted(() => {
    window.clearInterval(timeId)
  })

  return { form, time, send }
}
